package kr.co.hk.coupon;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import kr.co.hk.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.Toast;

public class WebViews extends Activity {
	WebView mWeb;
public void onCreate(Bundle savedInstanceState){
	super.onCreate(savedInstanceState);
	setContentView(R.layout.coupon_webview);
	
	Intent intent = getIntent();
	String url = intent.getStringExtra("url");
	String html2 = getHtml(url);
	String html3 =html2.substring(html2.indexOf("<!-- 본문")+18, html2.indexOf("<!-- 쿠폰 사용 설명 -->"));
	mWeb = (WebView)findViewById(R.id.coupon_webview);
	mWeb.clearCache(true);
	mWeb.getSettings().setSaveFormData(false);
	mWeb.getSettings().setBuiltInZoomControls(true);
	mWeb.loadDataWithBaseURL(url, html3, "text/html", "utf-8", null);
}
private String getHtml(String addr) {
	StringBuilder html = new StringBuilder();
	try {
		URL url = new URL(addr);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoOutput(true);
		if (conn != null) {
			conn.setConnectTimeout(10000);
			conn.setReadTimeout(10000);
			conn.setUseCaches(false);
			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				BufferedReader br = new BufferedReader(
						new InputStreamReader(conn.getInputStream(),
								"euc-kr"));
				for (;;) {
					String line = br.readLine();
							 html.append(line+"\n");
					if(line.contains("</html>"))
						break;
				}
				br.close();
			}
			conn.disconnect();
		}
	} catch (Exception ex) {
		Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
	}
	return html.toString();
}
}
